﻿$(document).ready(function(e) {
	//用于保存openId
	var openId = window.sessionStorage.getItem('kmOp');
	//能否进行抽奖
	var canPlay = false;
	//抽奖次数
	prizeCount = 0;
	//奖品列表
	//var prizeArr = ['LED随身灯','手机指环','50积分','暖风机','抱枕被','10元以内配件','运动手环'];
	var prizeArr = ['手机指环','LED随身灯','运动手环','10元以内配件','抱枕被','暖风机','50积分'];
	
	var s = layer.open({type:2, shadeClose: false});
	var apiUrl="http://219.135.99.176:58285/lotteryAPI/weixin/WXActivity/Posthandler.ashx";
	/*抽奖逻辑部分*/
	var bRotate = false;	
	var rotateFn = function (awards, angles, index){
		bRotate = !bRotate;
		$('#rotate').stopRotate();
		$('#rotate').rotate({
			angle:0,
			animateTo:angles+1800,
			duration:8000,
			callback:function (){
				bRotate = !bRotate;
				//次数减少
				prizeCount--;
				$('#pay_num').text(prizeCount);
				//显示中奖信息
				$('.pic').attr('src','images/prize/' + index +'.jpg');
				$('.prize-name').text(prizeArr[index]);
				$('#shade').fadeIn();
				$('.prize').fadeIn();
			}
		})
	};
	
	//点击抽奖按钮
	$('.pointer').click(function (){
		if(!canPlay) return;
		if(bRotate)return;
		if(prizeCount == 0){
			$('#shade').fadeIn();
			$('.ticket').fadeIn();
			return false;
		}
		//执行抽奖
		s = layer.open({type:2, shadeClose: false});
		$.post(apiUrl,{
			action: "Lottery", 
			openId: openId
		},function (data) {
			layer.close(s);
			if(data.Success == false){
				layer.open({content: '抽奖时出错' ,skin: 'msg' ,time: 2});
			}else{
				if(data.Data.IsSuc == false){
					if(data.Data.Code == 0){
						$('#shade').fadeIn();
						$('.end').fadeIn();
					}else{
						layer.open({content: data.Data.Message ,skin: 'msg' ,time: 2});
					}
					return false;
				}
				var item = parseInt(data.Data.Message);
				if(isNaN(item)) 
					return false;
				
				item--;
				//奖品列表
				/*
				switch (item) {
					case 0:
						rotateFn(0, 308, item);
						break;
					case 1:
						rotateFn(1, 360, item);
						break;
					case 2:
						rotateFn(2, 51, item);
						break;
					case 3:
						rotateFn(3, 104, item);
						break;
					case 4:
						rotateFn(4, 156, item);
						break;
					case 5:
						rotateFn(5, 208, item);
						break;
					case 6:
						rotateFn(6, 256, item);
						break;
				}
				*/
				switch (item) {
					case 0:
						rotateFn(0, 360, item);
						break;
					case 1:
						rotateFn(1, 308, item);
						break;
					case 2:
						rotateFn(2, 256, item);
						break;
					case 3:
						rotateFn(3, 208, item);
						break;
					case 4:
						rotateFn(4, 156, item);
						break;
					case 5:
						rotateFn(5, 104, item);
						break;
					case 6:
						rotateFn(6, 51, item);
						break;
				}
			}
		});
	});
	
	//关闭弹出框
	$('.close').click(function(){
		$('#shade').fadeOut();
		$('.tipsBox').fadeOut();
	});
	
	//弹出活动规则
	$('.activity_rule').click(function(){
		if(bRotate)return;
		$('#shade').fadeIn();
		$('.rule').fadeIn();
	});
	
	//弹出中奖纪录
	$('.prize_record').click(function(){
		if(bRotate)return;
		getPrizeList();
		$('#shade').fadeIn();
		$('.record').fadeIn();
	});
	
	//点击去领奖
	$('.get-prize').click(function(){
		getPrizeList();
		$('.tipsBox').hide();
		$('.record').fadeIn();
	});
	
	//用户是否已注册
	var checkUser = function(){
		s = layer.open({type:2, shadeClose: false});
		$.post(apiUrl,{
			action: "CheckUser", 
			openid: openId 
		},function (data) {
			layer.close(s);
			if(data.Success == false){
				layer.open({content: '判断用户注册出错' ,skin: 'msg' ,time: 2});
			}else{
				//判断是否需要弹框提示用户先去注册
				if(!data.Data.isRegister){
					$('#shade').fadeIn();
					$('.register').fadeIn();
					return false;
				}
				canPlay = data.Data.isRegister;
				prizeCount = data.Data.Count;
				$('#pay_num').text(prizeCount);
			}
		});
	}

	//获取地址栏的code
	var code = Y.getUrlParam('code');
	//获取openId
	if(Y.checkNull(openId)){
		$.post(apiUrl,{
			action: "GetOpenID", 
			code: code 
		},function (data) {
			layer.close(s);
			if(data.Success == false){
				layer.open({content: '获取openId失败' ,skin: 'msg' ,time: 2});
			}else{
				if(!Y.checkNull(data.Data)){
					openId = data.Data;
					window.sessionStorage.setItem('kmOp',openId);
					//执行判断用户是否已注册
					if(!Y.checkNull(openId)){
						checkUser();
						return false;
					}
				}
				layer.open({content: '获取openId失败' ,skin: 'msg' ,time: 2});
			}
		});
	}else{
		layer.close(s);
		checkUser();
	}
	
	//验证小票
	$('.submit-ticket').click(function(){
		var poscode = $('.input-ticket').val();
		if(Y.checkNull(poscode.trim())){
			layer.open({content: '请输入小票号' ,skin: 'msg' ,time: 2});
			return false;
		}
			
		s = layer.open({type:2, shadeClose: false});
		$.post(apiUrl,{
			action: "CheckNote", 
			openid: openId,
			poscode: poscode
		},function (data) {
			layer.close(s);
			if(data.Success == false){
				layer.open({content: '验证小票出错' ,skin: 'msg' ,time: 2});
			}else{
				if(data.Data > 0){
					prizeCount++;
					$('#pay_num').text(prizeCount);
					return false;
				}
				
				layer.open({content: '小票号码无效' ,skin: 'msg' ,time: 2});
			}
		});
	});
	
	var getPrizeList = function(){
		s = layer.open({type:2, shadeClose: false});
		$.post(apiUrl,{
			action: "LotteryList", 
			openId: openId,
		},function (data) {
			layer.close(s);
			if(data.Success == false){
				layer.open({content: '获取中奖列表出错' ,skin: 'msg' ,time: 2});
			}else{
				var list = data.Data;
				$('.record-item').empty();
				list.map(function(item,i){
					var html = '';
						html += '<li>';
						html += 	'<span class="item-container">';
						html +=			'<span class="item-num">'+ (i + 1) +'</span>';
						html += 		'<span class="item-name">'+ prizeArr[item.ProductType - 1] +'</span>';
						html +=			'<span class="item-date">'+ item.CreateDate.substring(0,10) +'</span>';
						html +=		'</span>';
						html +=	'</li>';
					$('.record-item').append(html);
				});
				$('.record-item').append('<li style="color:#e60012; font-size:1rem;">注：如抽中积分，请到会员中心查看</li>');
			}
		});
	}
});